package VUTHANHPHUC.SERIZABLE;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;

import NGUYENMINHHIEU.SERIZABLEFILES.Student;

public class Models<T> {
	
	 public boolean WriteListObjectIntoFiles(List<T> lisT, String resource, String fileName)
	            throws Exception {
	        try {
	            /*
	             * ClassLoader objcls =
	             * Thread.currentThread().getContextClassLoader(); URL url =
	             * objcls.getResource(resource);
	             */
	            String path = resource + "/" + fileName;
	            File objFile = new File(path);

	            FileOutputStream objOutFile = new FileOutputStream(objFile);
	            ObjectOutputStream objOoS = new ObjectOutputStream(objOutFile);
	            objOoS.writeObject(lisT);
	            objOoS.flush();
	        } catch (Exception e) {
	            throw e;
	        }
	        return true;
	    }

	    /***
	     * Nghiệp vụ đọc dữ liệu từ files --> List Object trong Java
	     * @param resource - Sample: abc/files/
	     * @param fileName - Sample: abcd.dat
	     * @return
	     * @throws Exception
	     */
	    public List<T> ReadListObjectIntoFiles(String resource, String fileName) throws Exception {
	        List<T> lisStudent = null;
	        try {
	            /*
	             * ClassLoader objcls =
	             * Thread.currentThread().getContextClassLoader(); URL url =
	             * objcls.getResource(resource);
	             */
	            String path = resource + "/" + fileName;
	            File objFile = new File(path);
	            ObjectInputStream objObjectIn = new ObjectInputStream(new FileInputStream(objFile));
	            lisStudent =  new ArrayList<T>();
	            lisStudent = (List<T>) objObjectIn.readObject();
	        } catch (Exception e) {
	            throw e;
	        }
	        return lisStudent;
	    }
}
